package com.jing.weibo.service.system;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jing.system.model.PageCls;
import com.jing.system.utils.StringUtil;
import com.jing.weibo.mapper.system.StationletterMapper;
import com.jing.weibo.pojo.system.Feedback;
import com.jing.weibo.pojo.system.Stationletter;
import com.jing.weibo.pojo.user.UserInfo;
import com.jing.weibo.service.user.UserInfoService;

/**
 * 站内信的Service
 * @author jing.yue
 * @version 2012/11/14 1.0.0
 */
@Component
public class StationletterService {

	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private StationletterMapper stationletterMapper;

	/**
	 * 保存站内信
	 * @param title			标题
	 * @param content		内容
	 * @param receusername	接收用户名
	 * @param sendusername	发送用户名
	 * @throws Exception
	 */
	public void saveStationletter(String title, String content, String receusername, String sendusername) throws Exception {
		saveStationletter(title, content, receusername, sendusername, null, null);
	}

	/**
	 * 保存站内信
	 * @param title			标题
	 * @param content		内容
	 * @param receusername	接收用户名
	 * @param sendusername	发送用户名
	 * @param recenickname	接收用户昵称
	 * @throws Exception
	 */
	public void saveStationletter(String title, String content, String receusername, String sendusername, String recenickname) throws Exception {
		saveStationletter(title, content, receusername, sendusername, recenickname, null);
	}

	/**
	 * 保存站内信
	 * @param title			标题
	 * @param content		内容
	 * @param receusername	接收用户名
	 * @param sendusername	发送用户名
	 * @param recenickname	接收用户昵称
	 * @param sendnickname	发送用户昵称
	 * @throws Exception
	 */
	public void saveStationletter(String title, String content, String receusername, String sendusername, String recenickname, String sendnickname) throws Exception {
		Stationletter stationletter = new Stationletter();
		if(StringUtil.isEmpty(recenickname)) {
			UserInfo receUserInfo = userInfoService.findByUserName(receusername);
			if(receUserInfo == null) {
				throw new Exception("系统不存在此接收用户!");
			}
			stationletter.setRecenickname(receUserInfo.getNickname());
		} else {
			stationletter.setRecenickname(recenickname);
		}
		if(StringUtil.isEmpty(sendnickname)) {
			UserInfo sendUserInfo = userInfoService.findByUserName(sendusername);
			if(sendUserInfo == null) {
				throw new Exception("系统不存在此发送用户!");
			}
			stationletter.setSendnickname(sendUserInfo.getNickname());
		} else {
			stationletter.setSendnickname(sendnickname);
		}
		stationletter.setTitle(title);
		stationletter.setContent(content);
		stationletter.setReceusername(receusername);
		stationletter.setSendusername(sendusername);
		stationletterMapper.saveStationletter(stationletter);
	}

	/**
	 * 获取未读站内信记录数
	 * @param username
	 * @return
	 * @throws Exception
	 */
	public Integer findUnStationletterCount(String username) throws Exception {
		return stationletterMapper.findUnStationletterCount(username);
	}

	/**
	 * 获取站内信
	 * @param stationletter
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> findStationletter(Stationletter stationletter) throws Exception {
		List<Feedback> stationletters = stationletterMapper.findStationletter(stationletter);
		Integer countSize = stationletterMapper.findStationletterCount(stationletter);
		PageCls pageCls = PageCls.setPageCls(stationletter.getPage(), stationletter.getSize(), null, null, countSize, stationletters.size(), stationletters);
		return pageCls.getPageInfo();
	}

	/**
	 * 根据ID获取站内信对象
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Stationletter findById(Integer id) throws Exception {
		return stationletterMapper.findById(id);
	}

	/**
	 * 根据ID修改查看状态为已查看
	 * @param id
	 * @throws Exception
	 */
	public void updateSeeStationletter(Integer id) throws Exception {
		stationletterMapper.updateSeeStationletter(id);
	}
}
